package com.study.flink.java.day03_windows;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

//事件型会话窗口，滑动窗口
//SocketSource->单行的source
public class SocketSourceEventTimeSlidingWindow {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 设置EventTime作为时间标准，数据所携带的时间
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);// 数据必须源源不断产生

        // 1582594382000,spark,3
        // 1582594383000,hadoop,2
        // 注意：对应并行source，每一个组都要满足条件才会触发
        DataStream<String> lines = env.socketTextStream("node02", 8888)
                .assignTimestampsAndWatermarks(
                // Watermarks允许数据迟到时间2秒，是Flink中窗口延迟触发机制
                // Watermarks = 数据所携带的时间（窗口中的最大时间） - 延迟执行的时间
                // Watermarks >= 上一个窗口的结束边界就会触发窗口执行
                new BoundedOutOfOrdernessTimestampExtractor<String>(Time.seconds(0)) {
            // 提取时间字段，返回Long类型
            @Override
            public long extractTimestamp(String line) {
                String[] fields = line.split(",");
                return Long.parseLong(fields[0]);
            }
        });

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndCount = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String s) throws Exception {
                // (单词,次数)
                String[] fields = s.split(",");
                return Tuple2.of(fields[1], Integer.parseInt(fields[2]));
            }
        });

        // 先分组，再划分窗口
        KeyedStream<Tuple2<String, Integer>, Tuple> keyed = wordAndCount.keyBy(0);
        // 划分窗口，组内分组满足条件再聚合打印，当前两条数据之差不超过的时间差
        WindowedStream<Tuple2<String, Integer>, Tuple, TimeWindow> window = keyed.window(SlidingEventTimeWindows.of(Time.seconds(6), Time.seconds(2))); // 注意窗口会自动对齐

        // 窗口中聚合
        SingleOutputStreamOperator<Tuple2<String, Integer>> summed = window.sum(1);

        summed.print();

        env.execute("SocketSourceEventTimeTumblingWindow-java");

    }




}
